package com.ryskoo.customer.applogin.repository;

import com.ryskoo.customer.applogin.domain.AppLoginTokenPO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Shawn
 */
@Repository
public class AppLoginTokenJPAImpl extends BasicJPAImpl<AppLoginTokenPO> implements IAppLoginTokenRepository {

	/**
	 * 根据 token和员工uuid 获取登录令牌
	 * @param token
	 * @param managerUuid
	 */
	@Override
	public AppLoginTokenPO getByTokenAndManager(String token,String managerUuid){
		String hql = "select o from AppLoginTokenPO o where 1=1 ";
		if(!StringUtils.isEmpty(token)){
			hql += " and o.accessToken = :accessToken ";
		}
		if(!StringUtils.isEmpty(managerUuid)){
			hql += " and o.managerUuid = :managerUuid ";
		}
		Query q = getH4Session().createQuery(hql);
		if(!StringUtils.isEmpty(token)){
			q.setString("accessToken", token);
		}
		if(!StringUtils.isEmpty(managerUuid)){
			q.setString("managerUuid", managerUuid);
		}
		
		List<AppLoginTokenPO> retList = q.list();
		if (retList != null && retList.size() > 0) {
			return retList.get(0);
		}
		return null;
	}
}
